package com.jiezuo.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiezuo.auth.entity.PositionLevel;
import com.jiezuo.auth.mapper.PositionLevelMapper;
import com.jiezuo.auth.service.PositionLevelService;
import com.jiezuo.common.exception.BusinessRuntimeException;
import com.jiezuo.common.util.StringUtil;
import com.jiezuo.common.util.VerifyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description 岗位等级
 * @author 庞留杰
 * @date 2024/03/19 17:21
 * @version V1.0
 **/
@Transactional
@Slf4j
@Service
public class PositionLevelServiceImpl extends ServiceImpl<PositionLevelMapper, PositionLevel> implements PositionLevelService {

    @Override
    public IPage<PositionLevel> findPage(Page initMpPage, PositionLevel positionLevel) {
        QueryWrapper<PositionLevel> queryWrapper = new QueryWrapper<>();
        if (StringUtil.isNotEmpty(positionLevel.getCode())) queryWrapper.like("code", positionLevel.getCode());
        if (StringUtil.isNotEmpty(positionLevel.getName())) queryWrapper.like("name", positionLevel.getName());
        queryWrapper.eq("is_delete", "0");
        queryWrapper.orderByAsc("level", "created_time");
        return this.page(initMpPage, queryWrapper);
    }

    @Override
    public List<PositionLevel> findList(PositionLevel positionLevel) {
        QueryWrapper<PositionLevel> queryWrapper = new QueryWrapper<>();
        if (StringUtil.isNotEmpty(positionLevel.getCode())) queryWrapper.like("code", positionLevel.getCode());
        if (StringUtil.isNotEmpty(positionLevel.getName())) queryWrapper.like("name", positionLevel.getName());
        queryWrapper.eq("is_delete", "0");
        queryWrapper.orderByAsc("level", "created_time");
        List<PositionLevel> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public PositionLevel findById(String id) {
        return this.getById(id);
    }

    @Override
    public boolean add(PositionLevel positionLevel) {
        VerifyUtil.notEmpty("等级", positionLevel.getLevel());
        QueryWrapper<PositionLevel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("level", positionLevel.getLevel());
        queryWrapper.eq("is_delete", "0");
        List<PositionLevel> list = this.list(queryWrapper);
        if (list.size() > 0) {
            throw new BusinessRuntimeException("当前等级已存在，不允许重复添加");
        }
        return this.save(positionLevel);
    }

    @Override
    public boolean update(PositionLevel positionLevel) {
        VerifyUtil.notEmpty("等级", positionLevel.getLevel());
        QueryWrapper<PositionLevel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("level", positionLevel.getLevel());
        queryWrapper.eq("is_delete", "0");
        queryWrapper.ne("id", positionLevel.getId());//除去自己
        List<PositionLevel> list = this.list(queryWrapper);
        if (list.size() > 0) {
            throw new BusinessRuntimeException("当前等级已存在，不允许重复添加");
        }
        return this.updateById(positionLevel);
    }

    @Override
    public boolean delete(String id) {
        PositionLevel positionLevel = new PositionLevel();
        positionLevel.setId(id);
        positionLevel.setIs_delete("1");
        return this.updateById(positionLevel);
    }
}
